package cn.com.se.sharepictrue.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SwitchCompat;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.se.sharepictrue.R;
import cn.com.se.sharepictrue.application.Data;
import cn.com.se.sharepictrue.bean.UserInfoBean;
import cn.com.se.sharepictrue.business.CallBack;
import cn.com.se.sharepictrue.business.NetworkResponse;
import cn.com.se.sharepictrue.business.ShareApi;
import cn.com.se.sharepictrue.utils.DataCleanUtils;
import cn.com.se.sharepictrue.utils.DialogUtils;
import cn.com.se.sharepictrue.utils.StringUtils;
import cn.com.se.sharepictrue.utils.ToastUtils;

/**
 * 设置界面
 * Created by KIDNG on 2015/11/5.
 */
public class SettingActivity extends ActionBarActivity {
    @Bind(R.id.tb_setting_title)
    Toolbar mTbSettingTitle;
    @Bind(R.id.switch_setting_hide)
    SwitchCompat mSwitchHide;
    @Bind(R.id.tv_setting_cache_size)
    TextView mTvSettingCacheSize;
    @Bind(R.id.ll_setting_admin)
    LinearLayout mLlSettingAdmin;
    @Bind(R.id.ll_setting_mine)
    LinearLayout mLlSettingMine;
    UserInfoBean mUserInfoBean;
    private boolean noShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initToolbar();
        initView();
    }

    private void initToolbar() {
        setSupportActionBar(mTbSettingTitle);
        mTbSettingTitle.setTitleTextColor(Color.WHITE);
    }

    private void initView() {
        getCacheSize();
        if ((mUserInfoBean = ((Data) getApplication()).getUserInfo()) != null) {
            if (mUserInfoBean.getIsAllow() != null && mUserInfoBean.getIsShow().equals(UserInfoBean.IS_SHOW)) {
                mSwitchHide.setChecked(false);
            } else {
                mSwitchHide.setChecked(true);
            }
            if (mUserInfoBean.isAdmin()) {
                mLlSettingAdmin.setVisibility(View.VISIBLE);
            }
            if (!StringUtils.isEmpty(mUserInfoBean.getUserID())) {
                mLlSettingMine.setVisibility(View.VISIBLE);
            }
        }
    }

    private void getCacheSize() {
        mTvSettingCacheSize.setText(DataCleanUtils.getTotalCacheSize(this));
    }

    @OnClick({R.id.ll_setting_clear, R.id.ll_setting_ban,
            R.id.ll_setting_about, R.id.ll_setting_mine_info})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_setting_clear:
                DialogUtils.getConfirmDialog(this, "确认清除？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DataCleanUtils.clearAllCache(SettingActivity.this);
                        getCacheSize();
                    }
                }).show();
                break;
            case R.id.ll_setting_ban:
                Intent banIntent = new Intent(this, ManagerUserActivity.class);
                startActivity(banIntent);
                break;

//            case R.id.ll_setting_hind_key:
//                Intent hideIntent = new Intent(this, HideKeyActivity.class);
//                startActivity(hideIntent);
//                break;
            case R.id.ll_setting_about:
                Intent aboutIntent = new Intent(this, AboutActivity.class);
                startActivity(aboutIntent);
                break;
            case R.id.ll_setting_mine_info:
                Intent mineInfoIntent = new Intent(this, MineInfoActivity.class);
                startActivity(mineInfoIntent);
                break;
        }
    }

    @OnClick(R.id.switch_setting_hide)
    public void toHideMineShare(View v) {
        noShow = ((SwitchCompat) v).isChecked();
        ShareApi.hideMyAllShare(this, noShow, new CallBack<NetworkResponse<String>>() {
            @Override
            public void onFail() {
                ToastUtils.show(SettingActivity.this, "网络访问失败咯");
                mSwitchHide.setChecked(!noShow);
            }

            @Override
            public void onResponse(NetworkResponse<String> response) {
                if (!response.isStatus()) {
                    ToastUtils.show(SettingActivity.this, response.getError());
                    mSwitchHide.setChecked(!noShow);
                } else {
                    mUserInfoBean.setIsShow(noShow ? "0" : "1");
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
